const Base = require('../rest.js');

module.exports = class extends Base
{

    constructor(ctx)
    {
        super(ctx);
        this._model = this.model('apiv1/application');
    }

    async uploadWorkAction()
    {
        let work_step_data = {};

        //作品表数据
        let data = this.ctx.state.uploadWorkData;
        data.PK_WorksGuid = think.uuid('v3');
        data.PK_CompetitionStepsGuid = data.stepguid;

        //中间表数据
        work_step_data.PK_WorksNoGuid = think.uuid('v3');
        work_step_data.FK_WorksGuid = data.PK_WorksGuid;
        work_step_data.FK_CompetitionSteps = data.PK_CompetitionStepsGuid;
        let addInfo = await this._model.addWorks(data, work_step_data)
        this.error(addInfo);

    }

    /**
     * put resource
     * @return {Promise} []
     */
    async postAction()
    {
        let a = 2;
        if (think.isObject(this.ctx.state.psrData))
        {
            let saveData = this.ctx.state.psrData;
            saveData.PK_ApplicationGuid = think.uuid('v3');
            let res = await this._model.add(saveData);
            if (res !== false)
            {
                return this.error(0, '添加成功');
            } else
            {
                return this.error(-1, '系统繁忙,请稍后再试')
            }
        }

        if (think.isObject(this.ctx.state.teamData))
        {
            let saveData = this.ctx.state.teamData;
            saveData.PK_ApplicationGuid = think.uuid('v3');
            //生成队员
            let menmberDate = this._getMenmberData(saveData.Menmber, saveData.PK_ApplicationGuid);
            let res = await this._model.add(saveData);
            let res1 = await this.model('apiv1/member').addMany(menmberDate);
            if (res !== false && res1 !== false)
            {
                return this.error(0, '添加成功');
            } else
            {
                return this.error(-1, '系统繁忙,请稍后再试')
            }
        }

    }

    _getMenmberData(data, applicationGuid)
    {
        let res = [];
        for (let index in data)
        {
            let temp = {};
            temp.PK_MemberGuid = think.uuid('v3');
            temp.FK_ApplicationGuid = applicationGuid;
            temp.Name = data[index];
            res.push(temp);
        }
        return res;
    }


};
